
function euler(f,a,b,ic,h)
%   Using Euler's Method
%   with given endpoints (a,b),
%   initial cond. -- ic 
%   and n partition.

%   in matlab prompt:  euler('name of fun',a,b,ic,h)

%INPUT:
%      f  -- given f(t,y)
%      [a,b] -- time interval
%      ic  -- initial condition
%      h -- time step
  
%OUTPUT:  approx solution w 


n=(b-a)/h;       %number of partition
w=zeros(n+1,1);  %initialize w
t(1)=a;       %note that matlab index starts at 1 (not 0)
w(1)=ic;     %initial condition

for i=2:n+1

    w(i)=w(i-1) + h*feval(f,w(i-1),t(i-1)); %main line in Euler's algo
    t(i)=a + (i-1)*h;                       %updating time step

end %for loop

plot(t,w,'m-.'); %plot w1, w2, ... , wn
w                %list w1, w2, ... , wn
legend('Euler with h=.2');
xlabel('t');
ylabel('y');







